/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: Frank
 * 系统版本: 1.0.0
 * 开发人员: Frank
 * 开发时间: 2019/10/17 17:45
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.message.model;

import com.spring.arch.common.jpa.model.AbstractLogicalModel;
import com.spring.arch.message.enums.BusinessType;
import com.spring.arch.message.enums.MessageSendStatus;
import com.spring.arch.message.enums.MessageType;
import com.spring.arch.common.jpa.model.AbstractLogicalModel;
import com.spring.arch.message.enums.BusinessType;
import com.spring.arch.message.enums.MessageSendStatus;
import com.spring.arch.message.enums.MessageType;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

/**
 * 消息实体
 *
 * @author Frank
 * @version 1.0.0.1
 * @since 2019/10/17 17:45
 */
@NoArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "msg_message")
public class Message extends AbstractLogicalModel {

    private static final long serialVersionUID = -17322872949784088L;

    /**
     * 用户手机号
     */
    @NotBlank
    @Size(max = 11)
    @Pattern(regexp = "^(1[3-9])\\d{9}$", message = "用户手机号码格式错误")
    @Column(nullable = false)
    private String mobile;

    /**
     * 业务类型
     */
    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(length = 32)
    private BusinessType businessType;

    /**
     * 消息类型 验证码、提醒
     */
    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(length = 32)
    private MessageType type;

    /**
     * 发送状态
     */
    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(length = 32)
    private MessageSendStatus status;

    /**
     * 失败原因
     */
    @Size(max = 1000)
    private String reason;
    /**
     * 流水号
     */
    @Column(length = 32)
    private String bizId;

    public Message(String mobile, BusinessType businessType, MessageType type, MessageSendStatus status) {
        this.mobile = mobile;
        this.businessType = businessType;
        this.type = type;
        this.status = status;
    }

    /**
     * 截取失败原因长度
     *
     * @param reason
     */
    public void setReason(String reason) {
        if(StringUtils.isNotBlank(reason) && reason.length() > 1000) {
            reason = reason.substring(0, 1000);
        }
        this.reason = reason;
    }
}
